﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace JiZiYan.Controllers
{
   [Authorize]
    public class HomeController : Controller
    {
        // GET: Home
        
        public ActionResult Index()
        {
            ViewBag.UserName = this.User.Identity.Name; //获取当前登录者的用户名信息，传递给视图 
            List<Modules> roles = new List<Modules>();//创建了一个空的所有页面集合  
            using (JiZiYanEntities2 db=new JiZiYanEntities2())
            {
                var username = db.Users.SingleOrDefault(x => x.LoginName == this.User.Identity.Name);
                //获取当前登录用户的所有
                var usernameid = db.UserRoles.Where(x => x.UserId == username.Id).ToList();
                //根据当前登录的用户查询到该用户能访问那些页面
                List<RoleModules> szx = new List<RoleModules>();  
                //创建一个角色对应页面的集合            
                foreach (var item in usernameid)
                {
                    var nameroles = db.RoleModules.Where(x => x.RoleId == item.RoleId).ToList();
                    //获取该用户能访问的页面和角色页面id一致的值
                    foreach (var item1 in nameroles)
                    {
                        szx.Add(item1);
                        //循环添加
                    }
                }
                List<Modules> cd = new List<Modules>();
                //创建空的集合
                foreach (var item in szx)
                {
                    var dq = db.Modules.SingleOrDefault(x => x.Id == item.ModuleId);
                    //角色页面表id和我当前用户能访问到的页面id一值的数组
                    cd.Add(dq);
                    //往角色页面表中添加
                }
                ViewBag.roles = cd.Distinct();/*去除重复项*/
                //返回给前台
            }   
            return View();
        }
        /// <summary>
        /// 登录页面视图
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]    
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <returns></returns>       
        [AllowAnonymous]
        [HttpPost]
        public ActionResult Logins(string name,string pwd)
        {
            using (JiZiYanEntities2 login =new JiZiYanEntities2())
            {
                var username = login.Users.FirstOrDefault(x => x.LoginName == name);
               //由于一个用户可以是多个身份，所以登录时返回第一个用户id即可
                if (username == null)
                {
                    return Json(new { success = false, msg = "用户名不存在" });                   
                }
                else if (username.Password!=pwd)
                {
                    return Json(new { success = false, msg = "密码不正确" });
                }
                else if (username.IsLockout == true)    //判断用户是否被禁用
                {                                       
                    return Json(new { success = false, msg = "您已经被禁用，请联系管理员解封" });
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(name, false);//颁发通行证
                    username.LastLoginTime = DateTime.Now;//获取当前时间赋值给数据库列
                    login.SaveChanges();//保存改变
                    ViewBag.loginname = username;
                    return Json(new { success = true, msg = "" });
                }
            }
        }
        /// <summary>
        /// 安全退出
        /// </summary>
        /// <returns></returns>       
        public ActionResult Back()
        {
            FormsAuthentication.SignOut();//注销通行证
            return Redirect("/Home/Login");  //转到登录页面
        }
        /// <summary>
        /// 欢迎页
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult welcome()
        {
            return View();
        }
    }
}